package com.cn.wanxi.jwt.service.impl;

import com.cn.wanxi.jwt.domain.User;
import com.cn.wanxi.jwt.mapper.UserMapper;
import com.cn.wanxi.jwt.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cn.wanxi.jwt.util.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 后台用户表 服务实现类
 * </p>
 *
 * @author 吉酱
 * @since 2022-09-27
 */
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;


    @Autowired
    private AuthenticationManager authenticationManager;

    @Override
    public int addUser(User user) {

        return userMapper.insert(user);
    }

    @Override
    public int deleteUserById(Integer id) {

        return userMapper.deleteById(id);
    }

    @Override
    public int updateUser(User user) {
        return userMapper.updateById(user);
    }

    @Override
    public User getUserById(Integer id) {

        return userMapper.selectById(id);

    }

    @Override
    public List<User> getAllUser(User user) {

        return userMapper.selectAll();
    }

    @Override
    public String login(User user) {
        //获取前端输入的用户名
        String username = user.getUsername();

        //当username为null是，断言生效即输出message
        Assert.notNull(username, "账号必须不能为空");

        //获取前端生成的密码
        String password = user.getPassword();

        Assert.notNull(password, "密码必须不能为空");

        //根据用户名查询用户
        User selectedUser = userMapper.selectByUsername(username);

        //该用户名不存在直接返回null
        if (selectedUser == null) {
            return null;
        }

        //判断用户输入的密码与数据库中存储的密码是否匹配
        boolean matches = passwordEncoder.matches(password, selectedUser.getPassword());

        //如果匹配 -->说明登录成功
        if (matches) {
            //返回生成的jwt
            return jwtTokenUtil.generateToken(user);
        }
        return null;
    }

    @Override
    public String loginBySecurity(User user) {

        //这里会根据token的类型，去选择合适的provider，
        // 然后调用对应的security中的service生成一个认证对象
        UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(user.getUsername(),
                user.getPassword());

        //获取生成的认证对象
        Authentication authentication = authenticationManager.authenticate(token);

        //如果授权对象为null
        if (Objects.isNull(authentication) || Objects.isNull(authentication.getPrincipal())) {
            return null;
        }

        //TODO 将权限对象存放到redis

        //获取到登录的用户
        User loginUser = (User) authentication.getPrincipal();

        System.out.println(authentication);

        return jwtTokenUtil.generateToken(loginUser);
    }


}


